import java.io.IOException;
import java.util.Scanner;

// main class firing the input
public class StartVM {

	// here we go
	public static void main(String[] args) throws IOException, InterruptedException {
		
		// shortcut
		if(args.length == 1) {
			Monitor monitor = new Monitor();
			monitor.countWords(args[0]);
			System.exit(0);
		}
		
		// initialize monitor and input scanner
		System.out.println("Initializing...");
		Monitor monitor = new Monitor();
		Scanner scan = new Scanner(System.in);
		System.out.println("Running...");
		System.out.println("Type `help` for options");
		
		// constantly check for user input
		while(true) {
			System.out.print("IN4392 $: ");
			String input = scan.nextLine();
			String[] inputArray = input.split(" ");
			// first word is the command, parameters are after the command 
			String command = inputArray[0];
			
			// as of 1.7, a switch can be used. Unfortunately, I don't use 1.7, so dirty if else statements it is!
			if(command.equals("show")) {
				monitor.showVMs();
				// list the VMs
			} else if(command.equals("add")) {
				// add (multiple) new virtual machines
				if(inputArray[1] != null)  
					monitor.addVMs(Integer.parseInt(inputArray[1]));
				else System.out.println("The add command requires an integer parameter.");
				
			} else if(command.equals("remove")) {
				// shutdown (multiple) virtual machines
				if(inputArray[1] != null) 
					monitor.removeVMs(Integer.parseInt(inputArray[1]));
				else System.out.println("The add command requires an integer parameter.");
			} else if(command.equals("exit")) {
				// exit the program
				System.out.println("byebye");
				System.exit(0);
			} else if(command.equals("count")) {
				// count vowels of a file
				if(inputArray[1] != null)  
					monitor.countWords(inputArray[1]);
				else System.out.println("The count command requires a filename.");
			} else if(command.equals("clear")) {
				// clear failed virtual machines (lets hope we will not need this!)
				monitor.clear();
			} else if(command.equals("kill")){
				monitor.removeAllVMs();
			} else {
				System.out.println(
						"+------------------------------------------------------+\n" +
						"| Welcome to IN4392 CC DAS4 Java Environment           |\n" + 
						"| Created by Sijmen Hoogendijk & Rutger Plak           |\n" +
						"+------------------------------------------------------+\n" + 
						"| show         -   shows all running virtual machines  |\n" +
						"| add x        -   adds x virtual machines             |\n" +
						"| remove x     -   shuts down x virtual machines       |\n" +
						"| count x      -   count number of vowels in file x    |\n" +
						"| clear        -   clear all failed virtual machines   |\n" +
						"| exit         -   quit program                        |\n" +
						"| help         -   shows this message                  |\n" +
						"+------------------------------------------------------+"
				);
			}
		}
	}
}
